<?php
session_start();
session_start();
//假设用户登录成功获得了以下用户数据
$userinfo = array(
    'user_id'  => 10000,
    'username'  => 'spark',
    'email' => 'spark@imooc.com',
    'sex'  => 'man',
    'age'  => '18'
);
header("content-type:text/html; charset=utf-8");

/* 将用户信息保存到session中 */
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['username'] = $userinfo['username'];
$_SESSION['userinfo'] = $userinfo;
echo '<br /><a href="text.php?' . SID . '">text</a>';
if(isset($_POST["submit"]) && $_POST["submit"] == "登陆")
{
    $user = $_POST["username"];
    $psw = $_POST["password"];
    if($user == "" || $psw == "")
    {
        echo "<script>alert('请输入用户名或密码！'); history.go(-1);</script>";
    }
    else
    {
        $con=mysqli_connect("localhost","root","123456","zb");
        $result = mysqli_query($con,"select username,password from login where username = $_POST[username] and password = $_POST[password]");
        $num = mysqli_num_rows($result);
        if($num)
        {
            $row = mysqli_fetch_array($result);  //将数据以索引方式储存在数组中
            $_SESSION["username"]=$_POST["username"];
//注册session变量，赋值为一个用户的ID
            $_SESSION["id"]=mysqli_query($con,"select username from login where username = $_POST[username]");
            echo "<script>alert('登陆成功！');window.location.href='text.html';</script>";
        }
        else
        {
            echo "<script>alert('用户名或密码不正确！');history.go(-1);</script>";
        }
    }
}
else
{
    echo "<script>alert('提交未成功！'); history.go(-1);</script>";
}

?>